﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Air_condition.DO;
using Air_condition.BU;
using Com.BU;
using Com.DA;

namespace Air_condition.GUI
{
    /// <summary>
    /// Interaction logic for ucAirConditioning.xaml
    /// </summary>
    public partial class ucAirConditioning : UserControl
    {
        AlternativeManager altman = new AlternativeManager();
        Alternative alt = new Alternative();
        Room room = new Room();
        Boolean ceilling = true;
        Boolean blroof = true;
        Boolean floor = true;
        tb_heat_by_man manheat;
        tb_air_conditioning_place conplace;
        Equip equip;
        Wall wall;
        Roof roof;
        Ceiling ce;
        Floor fl = new Floor();
        List<string> lLatitute = new List<string>();
        List<int> lSolarHour = new List<int>();
        List<int> lMonth = new List<int>();
        List<int> lTime = new List<int>();
        List<int> lPressure = new List<int>();

        int iEquipment = 1;
        int iWall = 1;
        int iRoof = 1;
        int iRoom = 1;
        #region LOAD
        public ucAirConditioning()
        {
            InitializeComponent();
            addAlt("Alternative 1");
            alt = altman.lAlternative.SingleOrDefault(a => a.sName == "Alternative 1");
            LoadEquipment();
            LoadSpace();
            LoadTypeOfGlass();
            LoadGlassDirection();
            LoadPressure();
            LoadTime();
            LoadLatitute();
            LoadSolarHour();
            LoadMonth();
            LoadCeilling();
            LoadCeillingLayer();
            LoadStructureRoof();
            LoadCharacteristicRoof();
            LoadStructureFloor();
            LoadCharacteristicFloor();
            LoadRoofSurface();
            LibraryManagerInCom.loadHeatByMan();
            LibraryManagerInCom.loadExitingAir();
            cbxGender.Items.Add("Male");
            cbxGender.Items.Add("Female");
            cbxGender.Items.Add("Child");
            setData();
        }


        public void setData()
        {
            //  Latitue
            lLatitute.Add("10 deg North");
            lLatitute.Add("20 deg North");
            lLatitute.Add("30 deg North");


            //  Solar Hour
            for (int i = 6; i <= 17; i++)
            {
                lSolarHour.Add(i);
            }
            //  Month
            for (int i = 1; i <= 12; i++)
            {
                lMonth.Add(i);
            }
            //  Time
            for (int i = 1; i <= 24; i++)
            {
                lTime.Add(i);
            }
            //  Pressure
            lPressure.Add(150);
            lPressure.Add(500);
            lPressure.Add(700);

            cbxColor.Items.Add("Dark Color");
            cbxColor.Items.Add("Light Color");
            cbxColor.Items.Add("Average Color");
            cbxColor.SelectedIndex = 0;
        }

        public void LoadSpace()
        {
            cbxSpaceConditioning.ItemsSource = null;
            cbxSpaceConditioning.Items.Clear();
            LibraryManagerInCom.loadAirConditionPlace();
            cbxSpaceConditioning.ItemsSource = LibraryManagerInCom.AirConditionPlaceList;
            cbxSpaceConditioning.DisplayMemberPath = "description";
            //cbxSpaceConditioning.SelectedIndex = 0;
        }

        public void LoadEquipment()
        {
            cbxTypeOfEquipment.ItemsSource = null;
            cbxTypeOfEquipment.Items.Clear();
            LibraryManagerInCom.loadEquipment();
            cbxTypeOfEquipment.ItemsSource = LibraryManagerInCom.EquipmentList;
            cbxTypeOfEquipment.DisplayMemberPath = "equipment_name";
        }
        //
        // Load loại kính
        public void LoadTypeOfGlass()
        {
            cbxTypeOfGlass.ItemsSource = null;
            cbxTypeOfGlass.Items.Clear();
            LibraryManagerInCom.loadTypeOfGlass();
            cbxTypeOfGlass.ItemsSource = LibraryManagerInCom.GlassCoeffientList;
            cbxTypeOfGlass.DisplayMemberPath = "glass_type";
            cbxTypeOfGlass.SelectedIndex = -1;
        }
        //  Load hướng của tường
        public void LoadGlassDirection()
        {
            cbxDirection.ItemsSource = null;
            cbxDirection.Items.Clear();
            LibraryManagerInCom.loadDirectionOfGlass();
            cbxDirection.ItemsSource = LibraryManagerInCom.GlassDirectionList;
            cbxDirection.DisplayMemberPath = "glass_direction";
            cbxDirection.SelectedIndex = -1;
        }
        //  Load số Pressure kg/m2 sàn
        public void LoadPressure()
        {
            cbxPressure.ItemsSource = lPressure;
            cbxPressure.SelectedIndex = -1;
        }
        //  Load thời gian
        public void LoadTime()
        {
            cbxTime.ItemsSource = lTime;
            cbxTime.SelectedIndex = -1;
        }

        // Load cấu tạo của trần và sàn
        public void LoadCeilling()
        {

            cbxStructureOfCeilling.ItemsSource = null;
            cbxStructureOfCeilling.Items.Clear();
            LibraryManagerInCom.loadCeilling();
            cbxStructureOfCeilling.ItemsSource = LibraryManagerInCom.CeilingList;
            cbxStructureOfCeilling.DisplayMemberPath = "description";
            cbxStructureOfCeilling.SelectedIndex = -1;

        }
        //  Load cấu tạo của sàn
        public void LoadStructureFloor()
        {
            cbxStructureOfFloor.ItemsSource = null;
            cbxStructureOfFloor.Items.Clear();
            LibraryManagerInCom.loadCeilling();
            cbxStructureOfFloor.ItemsSource = LibraryManagerInCom.CeilingList;
            cbxStructureOfFloor.DisplayMemberPath = "description";
            cbxStructureOfFloor.SelectedIndex = -1;

        }
        // Load đặc điểm của trần
        public void LoadCeillingLayer()
        {
            cbxCharacteristicsOfCeilling.ItemsSource = null;
            cbxCharacteristicsOfCeilling.Items.Clear();
            LibraryManagerInCom.loadCeillingLayer();
            cbxCharacteristicsOfCeilling.ItemsSource = LibraryManagerInCom.CeilingLayerList;
            cbxCharacteristicsOfCeilling.DisplayMemberPath = "layer_type";
            cbxCharacteristicsOfCeilling.SelectedIndex = -1;

        }

        //  Load đặc điểm của sàn
        public void LoadCharacteristicFloor()
        {
            cbxCharacteristicsOfFloor.ItemsSource = null;
            cbxCharacteristicsOfFloor.Items.Clear();
            LibraryManagerInCom.loadCeillingLayer();
            cbxCharacteristicsOfFloor.ItemsSource = LibraryManagerInCom.CeilingLayerList;
            cbxCharacteristicsOfFloor.DisplayMemberPath = "layer_type";
            cbxCharacteristicsOfFloor.SelectedIndex = -1;
        }

        //  Load cấu tạo của mái
        public void LoadStructureRoof()
        {
            cbxStructureOfRoof.ItemsSource = null;
            cbxStructureOfRoof.Items.Clear();
            LibraryManagerInCom.loadStructureRoof();
            cbxStructureOfRoof.ItemsSource = LibraryManagerInCom.RoofCeilingStructureList;
            cbxStructureOfRoof.DisplayMemberPath = "roof_ceiling_structure";
            cbxStructureOfRoof.SelectedIndex = 0;
        }
        //  Load đặc điểm của mái
        public void LoadCharacteristicRoof()
        {
            cbxCharacteristicsOfRoof.ItemsSource = null;
            cbxCharacteristicsOfRoof.Items.Clear();
            LibraryManagerInCom.loadCharacteristicRoof();
            cbxCharacteristicsOfRoof.ItemsSource = LibraryManagerInCom.RoofCeilingCharacteristicList;
            cbxCharacteristicsOfRoof.DisplayMemberPath = "roof_ceiling_characteristic";
            cbxCharacteristicsOfRoof.SelectedIndex = 0;
        }
        //  Load loại mặt mái
        public void LoadRoofSurface()
        {
            cbxTypeOfSurface.ItemsSource = null;
            cbxTypeOfSurface.Items.Clear();
            LibraryManagerInCom.loadRoofSurface();
            cbxTypeOfSurface.ItemsSource = LibraryManagerInCom.RoofSurfaceList;
            cbxTypeOfSurface.DisplayMemberPath = "surface_roof";
            cbxTypeOfSurface.SelectedIndex = 0;
        }
        //  Load vĩ độ
        public void LoadLatitute()
        {
            cbxLatitute.ItemsSource = lLatitute;
            cbxLatitute.SelectedIndex = -1;

        }
        //  Load giờ mặt trời
        public void LoadSolarHour()
        {
            cbxSolarHour.ItemsSource = lSolarHour;
            cbxSolarHour.SelectedIndex = -1;
        }
        //  Load tháng
        public void LoadMonth()
        {
            cbxMonth.ItemsSource = lMonth;
            cbxMonth.SelectedIndex = -1;
        }
        #endregion

        #region ROOM
        // XỬ LÝ TAB ROOM

        public void SetOutsideTemperature(string sOutsideTemperature)
        {
            txtOutsideTemperature.Text = sOutsideTemperature;
        }

        private void txtInsideTemperature_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtInsideTemperature);
            CheckManHeat();
        }

        private void btnApplyRoom_Click(object sender, RoutedEventArgs e)
        {
            room.sName = txtName.Text;
            room.we.dbOutSideTemperature = Convert.ToDouble(txtOutsideTemperature.Text);
            room.dbOutsideAirContainingLevel = Convert.ToDouble(txtOutsideAirContainingLevel.Text);
            room.dbInsideAirContainingLevel = Convert.ToDouble(txtInsideAirContainingLevel.Text);
            room.dbWidth = Convert.ToDouble(txtWidthRoom.Text);
            room.dbHeigh = Convert.ToDouble(txtHeighRoom.Text);
            room.dbLength = Convert.ToDouble(txtLengthRoom.Text);
            room.dbVolume = room.dbWidth * room.dbHeigh * room.dbLength;
            tb_exiting_air ea = (tb_exiting_air)LibraryManagerInCom.ExitAirList.SingleOrDefault(ex => ex.room_volume == room.dbVolume);
            if (ea != null)
            {
                room.dbAirCoefficient = Convert.ToDouble(ea.coefficient);
            }
            room.dbInsideTemperature = Convert.ToDouble(txtInsideTemperature.Text);
            if (MyRoom != null)
            {
                MyRoom(room.sName, alt.sName, true);
            }
            alt.lRoom[alt.lRoom.IndexOf(room)] = room;
        }
        public void CheckRoom(bool b)
        {
            tabiInternalLoad.IsEnabled = b;
            tabConstruction.IsEnabled = b;
            btnApplyRoom.IsEnabled = b;
            btnCopyRoom.IsEnabled = b;
            btnDeleteRoom.IsEnabled = b;
        }
        // DELEGATE Tạo Mới RooM
        public delegate void AddRoom(string sroomname, string saltname, bool edit);
        public AddRoom MyRoom;

        private void btnNewRoom_Click(object sender, RoutedEventArgs e)
        {
            Room r = new Room();
            addRoom(r);
        }
        public void addRoom(object o)
        {
            Room r = (Room)o;
            bool check = true;
            if (txtName.Text != "")
            {
                foreach (Room ro in alt.lRoom)
                {
                    if (txtName.Text == ro.sName)
                    {
                        //MessageBox.Show("Room was existed");
                        r.sName = "Room " + iRoom;
                        check = false;
                    }
                }
                if (check)
                {
                    r.sName = txtName.Text;
                }
            }
            else
            {
                r.sName = "Room " + iRoom;
            }
            CheckRoom(true);
            alt.lRoom.Add(r);
            iRoom++;
            if (MyRoom != null)
            {
                MyRoom(r.sName, alt.sName, false);
            }
            txtName.Text = "";

        }
        public delegate void DeleteRoom(string sroomname, string saltname);
        public DeleteRoom DelRoom;
        private void btnDeleteRoom_Click(object sender, RoutedEventArgs e)
        {
            alt.lRoom.Remove(room);
            if (DelRoom != null)
            {
                DelRoom(room.sName, alt.sName);
            }
            //room.CalculateColdProductivity();

            //MessageBox.Show("SumColdProductivity: " + room.res.dbSumColdProductivity.ToString() +
            //                "\n - SumSensibleHeat: " + room.res.dbSumSensibleHeat.ToString() +
            //                "\n     + SumRoomSensibleHeat: " + room.res.dbSumRoomSensibleHeat.ToString() +
            //                "\n         * GlassRadiationHeat: " + room.res.dbGlassRadiationHeat.ToString() +
            //                "\n         * SumConstructionHeatTransfer: " + room.res.dbSumConstructionHeatTransfer.ToString() +
            //                "\n         * PeopleSensibleHeat: " + room.res.dbPeopleSensibleHeat.ToString() +
            //                "\n         * LightSensibleHeat: " + room.res.dbLightSensibleHeat.ToString() +
            //                "\n         * PowerToolsSensibleHeat: " + room.res.dbPowerToolsSensibleHeat.ToString() +
            //                "\n     + SumOutSideSensibleHeat: " + room.res.dbSumOutSideSensibleHeat.ToString() +
            //                "\n         * OutSideAirActiveSensibleHeat: " + room.res.dbOutSideAirActiveSensibleHeat.ToString() +
            //                "\n         * OutSideAirSensibleHeat: " + room.res.dbOutSideAirSensibleHeat.ToString() +
            //                "\n - SumLatentHeat: " + room.res.dbSumLatentHeat.ToString() +
            //                "\n     + SumRoomLatentHeat: " + room.res.dbSumRoomLatentHeat.ToString() +
            //                "\n     + SumOutSideLatentHeat: " + room.res.dbSumOutSideLatentHeat.ToString() +
            //                "\n         * OutSideAirActiveLatentHeat: " + room.res.dbOutSideAirActiveLatentHeat.ToString() +
            //                "\n         * OutSideAirLatentHeat: " + room.res.dbOutSideAirLatentHeat.ToString());
        }

        private void btnCopyRoom_Click(object sender, RoutedEventArgs e)
        {
            if (room != null)
            {
                Room copyroom = (Room)room.Clone();
                addRoom(copyroom);
            }
        }
        public void loadRoom(string sroomname, string saltname)
        {
            alt = altman.lAlternative.SingleOrDefault(a => a.sName == saltname);
            room = alt.lRoom.SingleOrDefault(r => r.sName == sroomname);
            if (room != null)
            {
                // load tab room
                txtName.Text = room.sName;
                txtWidthRoom.Text = room.dbWidth.ToString();
                txtHeighRoom.Text = room.dbHeigh.ToString();
                txtLengthRoom.Text = room.dbLength.ToString();
                txtOutsideTemperature.Text = room.we.dbOutSideTemperature.ToString();
                txtInsideTemperature.Text = room.dbInsideTemperature.ToString();
                txtOutsideAirContainingLevel.Text = room.dbOutsideAirContainingLevel.ToString();
                txtInsideAirContainingLevel.Text = room.dbInsideAirContainingLevel.ToString();

                // load tab internal
                cbxSpaceConditioning.SelectedIndex = room.iType;
                txtNumberOfPeople.Text = room.inter.NumberPeople.ToString();
                cbxGender.SelectedIndex = room.inter.Gender;
                txtPeopleLatentHeat.Text = room.inter.LatentHeat.ToString();
                txtPeopleSensibleHeat.Text = room.inter.SensibleHeat.ToString();
                txtAmountAir.Text = room.inter.AirMountForOnePeople.ToString();
                LoadEquipList();

                // load tab construction
                cbxTypeOfFrameGlass.SelectedIndex = room.gl.iWindowFrameType;
                txtAreaOfDoor.Text = room.gl.dbArea.ToString();
                cbxTypeOfGlass.SelectedIndex = room.gl.iTypeofglass;
                txtCoefficientOfGlass.Text = room.gl.dbGlassCoefficient.ToString();
                cbxLatitute.SelectedIndex = room.gl.iLatitute;
                cbxSolarHour.SelectedIndex = room.gl.iSolarHour;
                cbxMonth.SelectedIndex = room.gl.iMonth;
                cbxDirection.SelectedIndex = room.gl.iDirection;
                cbxPressure.SelectedIndex = room.gl.iPressure;
                cbxTime.SelectedIndex = room.gl.iTime;
                txtSolarRadiation.Text = room.gl.dbSolarRadition.ToString();
                txtImmediate.Text = room.gl.dbImmediateCoefficient.ToString();

                if (ce != null)
                {
                }
                else
                {
                    foreach (Roof r in lstRoof.Items)
                    {
                        room.lConstruction.Add(r);
                    }
                }

                foreach (Wall w in lstWall.Items)
                {
                    room.lConstruction.Add(w);
                }

                txtWidthOfFloor.Text = fl.dbWidth.ToString();
                txtLengthOfFloor.Text = fl.dbLength.ToString();
                cbxStructureOfFloor.SelectedIndex = fl.iStructure;
                cbxCharacteristicsOfFloor.SelectedIndex = fl.iCharacteristics;
                txtHeatTransferOfFloor.Text = fl.dbHeatTransferCoefficient.ToString();
            }
        }
        #endregion

        #region INTERNALOAD
        // XỬ LÝ TAB INTERNAL LOAD

        private void cbxSpaceConditioning_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            conplace = (tb_air_conditioning_place)cbxSpaceConditioning.SelectedItem;
            txtNumberOfPeople.Text = conplace.people_density.ToString();
            txtAmountAir.Text = conplace.air_amount_per_perople.ToString();
            CheckManHeat();
        }
        private void cbxGender_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            CheckManHeat();
        }
        public void CheckManHeat()
        {
            if (conplace != null && cbxGender.SelectedItem != null)
            {
                double dbInsideTemperature = Convert.ToDouble(txtInsideTemperature.Text);
                manheat = LibraryManagerInCom.HeatByManList.SingleOrDefault(mh => mh.working_place_id == conplace.id && mh.working_place_temperature == dbInsideTemperature);
                if (manheat != null)
                {
                    if (cbxGender.SelectedItem == "Male")
                    {
                        txtPeopleLatentHeat.Text = manheat.latent_heat.ToString();
                        txtPeopleSensibleHeat.Text = manheat.sensible_heat.ToString();
                    }
                    else
                    {
                        if (cbxGender.SelectedItem == "Female")
                        {
                            txtPeopleLatentHeat.Text = (manheat.latent_heat * 0.85).ToString();
                            txtPeopleSensibleHeat.Text = (manheat.sensible_heat * 0.85).ToString();
                        }
                        else
                        {
                            txtPeopleLatentHeat.Text = (manheat.latent_heat * 0.75).ToString();
                            txtPeopleSensibleHeat.Text = (manheat.sensible_heat * 0.75).ToString();
                        }
                    }
                }
            }
        }

        public void EnableEquipment(bool b)
        {
            txtEquipment.IsEnabled = b;
            cbxTypeOfEquipment.IsEnabled = b;
            lstEquipment.IsEnabled = b;
            txtEnergytOfEquipment.IsEnabled = b;
            btnCopyEquip.IsEnabled = b;
            btnDeleteEquip.IsEnabled = b;
        }
        private void btnEquip_Click(object sender, RoutedEventArgs e)
        {
            EnableEquipment(true);
            Equip equip = new Equip();
            addEquip(equip);
        }
        public void addEquip(object o)
        {
            Equip equip = (Equip)o;
            equip.sName = "Equipment " + iEquipment;
            iEquipment++;
            room.inter.lequip.Add(equip);
            LoadEquipList();
        }

        private void btnDeleteLoad_Click(object sender, RoutedEventArgs e)
        {
            room.inter.lequip.Remove(equip);
            LoadEquipList();
            if (lstEquipment.Items.Count == 0)
            {
                EnableEquipment(false);
            }

        }

        private void btnCopyEquip_Click(object sender, RoutedEventArgs e)
        {
            if (equip != null)
            {
                Equip equipment = (Equip)equip.Clone();
                addEquip(equipment);
            }
        }

        private void lstEquipment_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            equip = (Equip)lstEquipment.SelectedItem;
            if (equip != null)
            {
                txtEquipment.Text = equip.sName;
                cbxTypeOfEquipment.SelectedIndex = equip.iType;
                txtEnergytOfEquipment.Text = equip.dbEnergy.ToString();
            }
        }

        private void btnApplyInternalLoad_Click(object sender, RoutedEventArgs e)
        {
            if (txtNumberOfPeople.Text != "0")
            {
                room.inter.NumberPeople = (Convert.ToInt32(txtWidthRoom.Text) * Convert.ToInt32(txtLengthRoom.Text)) / Convert.ToInt32(txtNumberOfPeople.Text);
            }
            room.iType = cbxSpaceConditioning.SelectedIndex;
            room.inter.Gender = cbxGender.SelectedIndex;
            room.inter.LatentHeat = Convert.ToDouble(txtPeopleLatentHeat.Text);
            room.inter.SensibleHeat = Convert.ToDouble(txtPeopleSensibleHeat.Text);
            room.inter.AirMountForOnePeople = Convert.ToDouble(txtAmountAir.Text);
            if (equip != null)
            {
                if (txtEquipment.Text != "")
                {
                    equip.sName = txtEquipment.Text;
                }
                equip.iType = cbxTypeOfEquipment.SelectedIndex;
                equip.dbEnergy = Convert.ToDouble(txtEnergytOfEquipment.Text);
            }
            LoadEquipList();
        }
        public void LoadEquipList()
        {
            lstEquipment.ItemsSource = null;
            lstEquipment.Items.Clear();
            lstEquipment.ItemsSource = room.inter.lequip;
            lstEquipment.DisplayMemberPath = "sName";
        }

        private void cbxTypeOfEquipment_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            tb_equipment equipment = (tb_equipment)cbxTypeOfEquipment.SelectedItem;
            txtEnergytOfEquipment.Text = equipment.energy.ToString();
        }
        #endregion

        #region CONSTRUCTION
        // Load hệ số truyền nhiệt của ceiling
        private void cbxStructureOfCeilling_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ceilling)
            {
                LoadCeillingLayer();
                ceilling = false;
            }
            loadCeilling();
        }

        private void cbxCharacteristicsOfCeilling_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadCeilling();

        }

        private void loadCeilling()
        {
            if (cbxStructureOfCeilling.SelectedIndex != -1 && cbxCharacteristicsOfCeilling.SelectedIndex != -1)
            {
                LibraryManagerInCom.loadCeilingCoefficient();
                tb_ceiling ce = (tb_ceiling)cbxStructureOfCeilling.SelectedItem;
                tb_ceiling_layer l = (tb_ceiling_layer)cbxCharacteristicsOfCeilling.SelectedItem;
                tb_heat_transfer_ceiling tr = LibraryManagerInCom.CeilingCoefficientList.SingleOrDefault(c => c.ceiling_id == ce.id && c.layer_id == l.id);

                if (tr != null)
                {
                    txtHeatTransfer.Text = tr.coefficient.ToString();


                }
            }

        }

        //  Load hệ số truyền nhiệt của sàn
        private void cbxStructureOfFloor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            //try
            //{

            if (floor)
            {
                LoadCharacteristicFloor();
                floor = false;
            }
            loadFloor();
            //}
            //catch (Exception)
            //{
            //    MessageBox.Show("sai");
            //}
        }

        private void cbxCharacteristicsOfFloor_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadFloor();
        }
        private void loadFloor()
        {

            if (cbxCharacteristicsOfFloor.SelectedIndex != -1 && cbxCharacteristicsOfFloor.SelectedIndex != -1)
            {
                LibraryManagerInCom.loadCeilingCoefficient();
                tb_ceiling floor = (tb_ceiling)cbxStructureOfFloor.SelectedItem;
                tb_ceiling_layer layerfloor = (tb_ceiling_layer)cbxCharacteristicsOfFloor.SelectedItem;
                tb_heat_transfer_ceiling tran = LibraryManagerInCom.CeilingCoefficientList.SingleOrDefault(c => c.ceiling_id == floor.id && c.layer_id == layerfloor.id);
                if (tran != null)
                {
                    txtHeatTransferOfFloor.Text = tran.coefficient.ToString();
                }
                //else
                //{
                //    txtHeatTransferOfFloor.Text = "Type here...";
                //    txtHeatTransferOfFloor.Focus();
                //    txtHeatTransferOfFloor.SelectAll();

                //}
            }

        }

        //  Load hệ số truyền nhiệt của roof
        private void cbxStructureOfRoof_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (blroof)
            {
                LoadCharacteristicRoof();
                blroof = false;
            }
            loadRoof();
        }

        private void cbxCharacteristicsOfRoof_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadRoof();

        }
        private void loadRoof()
        {
            if (cbxStructureOfRoof.SelectedIndex != -1 && cbxCharacteristicsOfRoof.SelectedIndex != -1)
            {
                LibraryManagerInCom.loadRoofCeilingCoefficient();
                tb_roof_ceiling_structure structureroof = (tb_roof_ceiling_structure)cbxStructureOfRoof.SelectedItem;
                tb_roof_ceiling_characteristic characroof = (tb_roof_ceiling_characteristic)cbxCharacteristicsOfRoof.SelectedItem;
                tb_roof_ceiling_heat_coefficient ro = LibraryManagerInCom.RoofCeilingHeatList.SingleOrDefault(r => r.structure_id == structureroof.id && r.characteristic_id == characroof.id);
                if (ro != null)
                {
                    txtHeatTransferOfRoof.Text = ro.heat_coefficient.ToString();
                }
                else
                {
                    txtHeatTransferOfRoof.Text = "Type here...";
                    txtHeatTransferOfRoof.Focus();
                    txtHeatTransferOfRoof.SelectAll();

                }
            }
        }

        private void cbxTypeOfSurface_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txtAbsorptionCoefficient.Text = ((tb_roof_surface)cbxTypeOfSurface.SelectedItem).absorption_coefficient.ToString();

        }

        private void cbxTypeOfGlass_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            txtCoefficientOfGlass.Text = ((tb_radiation_characteristics)cbxTypeOfGlass.SelectedItem).glass_coefficient.ToString();
        }

        //  Load Solar Radiation từ 4 combobox Latitute, Direction, Solar hour, Month đã được chọn

        private void loadSolarRadition()
        {
            if (cbxSolarHour.SelectedIndex != -1 && cbxDirection.SelectedIndex != -1 && cbxLatitute.SelectedIndex != -1 && cbxMonth.SelectedIndex != -1)
            {

                try
                {

                    int? strDirectioncbx = ((tb_glass_direction)cbxDirection.SelectedItem).id;
                    int? strSolarHourcbx = (int)cbxSolarHour.SelectedItem;
                    int? strMonthcbx = (int)cbxMonth.SelectedItem; ;
                    string strLatitutecbx = cbxLatitute.SelectedItem.ToString();

                    LibraryManagerInCom.loadSolarByRadiation();
                    tb_solar_radiation_through_glass ro = LibraryManagerInCom.SolarRadiationList.SingleOrDefault(r => r.solar_hour == strSolarHourcbx && r.direction_id == strDirectioncbx && r.month == strMonthcbx && r.latitute == strLatitutecbx);

                    if (ro != null)
                    {

                        txtSolarRadiation.Text = ro.radiation_amount.ToString();
                    }
                    else
                    {
                        txtSolarRadiation.Text = "Type here...";
                        txtSolarRadiation.Focus();
                        txtSolarRadiation.SelectAll();

                    }
                }
                catch (Exception)
                {
                    throw;

                }

            }
        }
        //  Load hệ số truyền nhiệt tức thời
        private void loadImmediateEffect()
        {
            if (cbxDirection.SelectedIndex != -1 && cbxPressure.SelectedIndex != -1 && cbxTime.SelectedIndex != -1)
            {


                try
                {

                    int? Direction = ((tb_glass_direction)cbxDirection.SelectedItem).id;
                    int? Pressure = (int)cbxPressure.SelectedItem;
                    int? Time = (int)cbxTime.SelectedItem;

                    LibraryManagerInCom.loadImmediateImpact();
                    tb_immediate_impact im = LibraryManagerInCom.ImmediateImpactList.SingleOrDefault(r => r.direction_id == Direction && r.time == Time && r.weight_per_meter_square == Pressure);
                    if (im != null)
                    {
                        // MessageBox.Show("abc");
                        txtImmediate.Text = im.coefficient.ToString();
                    }
                    else
                    {
                        txtImmediate.Text = "Type here...";
                        txtImmediate.Focus();
                        txtImmediate.SelectAll();

                    }

                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
        private void cbxLatitute_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            loadSolarRadition();


        }

        private void cbxSolarHour_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadSolarRadition();


        }

        private void cbxMonth_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadSolarRadition();


        }

        private void cbxDirection_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadSolarRadition();
            loadImmediateEffect();

        }
        private void cbxPressure_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadImmediateEffect();
        }
        private void cbxTime_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            loadImmediateEffect();
        }
        public void enableroof(bool b)
        {
            txtWidthOfRoof.IsEnabled = b;
            txtLengthOfRoof.IsEnabled = b;
            cbxStructureOfRoof.IsEnabled = b;
            cbxCharacteristicsOfRoof.IsEnabled = b;
            txtHeatTransferOfRoof.IsEnabled = b;
            cbxColor.IsEnabled = b;
            cbxTypeOfSurface.IsEnabled = b;
            txtAbsorptionCoefficient.IsEnabled = b;
            lblRoof.IsEnabled = b;
            lblWidthOfRoof.IsEnabled = b;
            lblLengthOfRoof.IsEnabled = b;
            lblStructureOfRoof.IsEnabled = b;
            lblCharacteristicsOfRoof.IsEnabled = b;
            lblHeatTransferOfRoof.IsEnabled = b;
            lblColor.IsEnabled = b;
            lblTypeOfSurface.IsEnabled = b;
            lblAbsorptionCoefficient.IsEnabled = b;
            lblAddRoof.IsEnabled = b;
            lblTagRoof.IsEnabled = b;
            txtTagRoof.IsEnabled = b;
            lstRoof.IsEnabled = b;
            btnDeleteRoof.IsEnabled = b;
            btnCopyRoof.IsEnabled = b;
        }
        public void enableceiling(bool b)
        {
            txtWidthOfCeilling.IsEnabled = b;
            txtHeighOfCeilling.IsEnabled = b;
            cbxStructureOfCeilling.IsEnabled = b;
            cbxCharacteristicsOfCeilling.IsEnabled = b;
            txtHeatTransfer.IsEnabled = b;
            lblCeiling.IsEnabled = b;
            lblWidthOfCeilling.IsEnabled = b;
            lblHeighOfCeilling.IsEnabled = b;
            lblStructureOfCeilling.IsEnabled = b;
            lblCharacteristicsOfCeilling.IsEnabled = b;
            lblHeatTransferOfCeilling.IsEnabled = b;
        }
        //  Check RadioButon ceilling được chọn
        private void radCeilling_Checked(object sender, RoutedEventArgs e)
        {
            btnAddRoof.IsEnabled = false;
            enableceiling(true);
            enableroof(false);
            ce = new Ceiling();
        }

        //  Check RadioButoon Roof được chọn
        private void radRoof_Checked(object sender, RoutedEventArgs e)
        {
            ce = null;
            btnAddRoof.IsEnabled = true;
            enableceiling(false);
            if (lstRoof.Items.Count != 0)
            {
                enableroof(true);
            }
        }
        public void checklstwall(bool b)
        {
            lblWall.IsEnabled = b;
            lblWidthOfWall.IsEnabled = b;
            txtWidthOfWall.IsEnabled = b;
            lblLengthOfWall.IsEnabled = b;
            txtLengthOfWall.IsEnabled = b;
            lblTypeOfExposure.IsEnabled = b;
            txtTypeOfExposure.IsEditable = b;
            lblAddWall.IsEnabled = b;
            lblTagWall.IsEnabled = b;
            txtTagWall.IsEnabled = b;
            lstWall.IsEnabled = b;
            btnDeleteWall.IsEnabled = b;
            btnCopyWall.IsEnabled = b;
        }
        //  Add wall
        private void btnNewWall_Click(object sender, RoutedEventArgs e)
        {
            checklstwall(true);
            Wall wa = new Wall();
            addWall(wa);
        }

        private void btnDeleteWall_Click(object sender, RoutedEventArgs e)
        {
            //room.lConstruction.Remove(wall);
            lstWall.Items.Remove(wall);
            if (lstWall.Items.Count == 0)
            {
                checklstwall(false);
            }
        }

        public void addWall(object o) {
            Wall wa = (Wall)o;
            wa.sName = "Wall " + iWall;

            iWall++;
            //room.lConstruction.Add(wa);
            lstWall.Items.Add(wa);
            lstWall.DisplayMemberPath = "sName";
        }
        private void btnCopyWall_Click(object sender, RoutedEventArgs e)
        {
            if (wall != null)
            {
                Wall wallcopy = (Wall)wall.Clone();
                addWall(wallcopy);
            }
        }
        
        private void lstWall_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            wall = (Wall)lstWall.SelectedItem;
            if (wall != null)
            {
                txtTagWall.Text = wall.sName;
                txtLengthOfWall.Text = wall.dbLength.ToString();
                txtWidthOfWall.Text = wall.dbWidth.ToString();
                txtTypeOfExposure.SelectedIndex = wall.Iindex;
            }

        }
        private void lstWall_GotFocus(object sender, RoutedEventArgs e)
        {

            if (wall != null)
            {
                if (txtTagWall.Text != "")
                {
                    wall.sName = txtTagWall.Text;
                }
                wall.dbLength = Convert.ToDouble(txtLengthOfWall.Text);
                wall.dbWidth = Convert.ToDouble(txtWidthOfWall.Text);
                wall.Iindex = txtTypeOfExposure.SelectedIndex;
                //room.lConstruction[room.lConstruction.IndexOf(wall)] = wall;
            }
        }
        public void addRoof(object o) {
            Roof ro = (Roof)o;
            ro.sName = "Roof " + iRoof;
            iRoof++;
            //room.lConstruction.Add(ro);
            lstRoof.Items.Add(ro);
            lstRoof.DisplayMemberPath = "sName";
        }
        private void btnAddRoof_Click(object sender, RoutedEventArgs e)
        {
            enableroof(true);
            Roof ro = new Roof();
            addRoof(ro);
        }
        private void btnDeleteRoof_Click(object sender, RoutedEventArgs e)
        {
            //room.lConstruction.Remove(roof);
            lstRoof.Items.Remove(roof);
            if (lstRoof.Items.Count == 0)
            {
                enableroof(false);
            }
        }
        private void btnCopyRoof_Click(object sender, RoutedEventArgs e)
        {
            if (roof != null)
            {
                Roof roofcopy = (Roof)roof.Clone();
                addRoof(roofcopy);
            }
        }
       
        private void lstRoof_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            roof = (Roof)lstRoof.SelectedItem;
            if (roof != null)
            {
                roof = (Roof)lstRoof.SelectedItem;
                txtTagRoof.Text = roof.sName;
                txtWidthOfRoof.Text = roof.dbWidth.ToString();
                txtLengthOfRoof.Text = roof.dbLength.ToString();
                cbxStructureOfRoof.SelectedIndex = roof.sStructure;
                cbxCharacteristicsOfRoof.SelectedIndex = roof.sCharacteristics;
                txtHeatTransferOfRoof.Text = roof.dbHeatTransferCoefficient.ToString();
                cbxColor.SelectedIndex = roof.Icolor;
                cbxTypeOfSurface.SelectedIndex = roof.Surface;
                txtAbsorptionCoefficient.Text = roof.AbsorptionCoefficient.ToString();
            }
        }
        private void lstRoof_GotFocus(object sender, RoutedEventArgs e)
        {
            if (roof != null)
            {
                if (txtTagRoof.Text != "")
                {
                    roof.sName = txtTagRoof.Text;
                }
                roof.dbWidth = Convert.ToDouble(txtWidthOfRoof.Text);
                roof.dbLength = Convert.ToDouble(txtLengthOfRoof.Text);
                roof.sStructure = cbxStructureOfRoof.SelectedIndex;
                roof.sCharacteristics = cbxCharacteristicsOfRoof.SelectedIndex;
                roof.dbHeatTransferCoefficient = Convert.ToDouble(txtHeatTransferOfRoof.Text);
                roof.Icolor = cbxColor.SelectedIndex;
                roof.Surface = cbxTypeOfSurface.SelectedIndex;
                roof.AbsorptionCoefficient = Convert.ToDouble(txtAbsorptionCoefficient.Text);
                //room.lConstruction[room.lConstruction.IndexOf(roof)] = roof;
            }
        }

        //  Apply Roof va Wall
        private void btnApplyConstruction_Click(object sender, RoutedEventArgs e)
        {

            room.gl.iWindowFrameType = cbxTypeOfFrameGlass.SelectedIndex;
            room.gl.dbArea = Convert.ToDouble(txtAreaOfDoor.Text);
            room.gl.iTypeofglass = cbxTypeOfGlass.SelectedIndex;
            room.gl.dbGlassCoefficient = Convert.ToDouble(txtCoefficientOfGlass.Text);
            room.gl.iLatitute = cbxLatitute.SelectedIndex;
            room.gl.iSolarHour = cbxSolarHour.SelectedIndex;
            room.gl.iMonth = cbxMonth.SelectedIndex;
            room.gl.iDirection = cbxDirection.SelectedIndex;
            room.gl.iPressure = cbxPressure.SelectedIndex;
            room.gl.iTime = cbxTime.SelectedIndex;
            room.gl.dbSolarRadition = Convert.ToDouble(txtSolarRadiation.Text);
            room.gl.dbImmediateCoefficient = Convert.ToDouble(txtImmediate.Text);

            if (ce != null)
            {
                ce.dbWidth = Convert.ToDouble(txtWidthOfCeilling.Text);
                ce.dbLength = Convert.ToDouble(txtHeighOfCeilling.Text);
                ce.iStructure = cbxStructureOfCeilling.SelectedIndex;
                ce.iCharacteristics = cbxCharacteristicsOfCeilling.SelectedIndex;
                ce.dbHeatTransferCoefficient = Convert.ToDouble(txtHeatTransfer.Text);
                room.lConstruction.Add(ce);
            }
            else { 
                foreach(Roof r in lstRoof.Items){
                    room.lConstruction.Add(r);
                }
            }

            foreach(Wall w in lstWall.Items){
                room.lConstruction.Add(w);
            }

            fl.dbWidth = Convert.ToDouble(txtWidthOfFloor.Text);
            fl.dbLength = Convert.ToDouble(txtLengthOfFloor.Text);
            fl.iStructure = cbxStructureOfFloor.SelectedIndex;
            fl.iCharacteristics = cbxCharacteristicsOfFloor.SelectedIndex;
            fl.dbHeatTransferCoefficient = Convert.ToDouble(txtHeatTransferOfFloor.Text);
            room.lConstruction.Add(fl);
        }
        #endregion

        #region RUYNGUYENVN



        // DEMO Kết QUả Tính TOán




        public void addAlt(string saltname)
        {
            Alternative alt = new Alternative();
            alt.sName = saltname;
            altman.AddAlternative(alt);
        }

        public void loadAlt(string saltname)
        {
            alt = altman.lAlternative.SingleOrDefault(a => a.sName == saltname);
        }
        #endregion

        #region JUST NUMBER
        //  Xử lý button chỉ cho nhập số
        public void CheckInput(object sender, KeyEventArgs e)
        {
            if ((e.Key >= Key.D0 && e.Key <= Key.D9 && e.Key != Key.Space) || e.Key == Key.OemPeriod)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        #endregion

        #region EMPTY VALUE

        public void CheckEmpty(TextBox txt)
        {
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
        }
        #region for room
        // Tab room
        private void txtWidthRoom_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWidthRoom);
        }

        private void txtHeighRoom_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtHeighRoom);
        }

        private void txtLengthRoom_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtLengthRoom);
        }


        private void txtOutsideTemperature_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtOutsideTemperature);
        }


        private void txtOutsideAirContainingLevel_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtOutsideAirContainingLevel);
        }

        private void txtInsideAirContainingLevel_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtInsideAirContainingLevel);
        }
        #endregion

        #region for internal
        private void txtNumberOfPeople_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtNumberOfPeople);
        }

        private void txtPeopleLatentHeat_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtPeopleLatentHeat);
        }


        private void txtPeopleSensibleHeat_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtPeopleSensibleHeat);
        }

        private void txtAmountAir_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtAmountAir);
        }
        #endregion

        #region for construction
        private void txtAreaOfDoor_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtAreaOfDoor);
        }

        private void txtCoefficientOfGlass_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtCoefficientOfGlass);
        }


        private void txtSolarRadiation_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtSolarRadiation);
        }

        private void txtImmediate_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtImmediate);
        }
        private void txtWidthOfCeilling_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWidthOfCeilling);
        }

        private void txtHeighOfCeilling_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtHeighOfCeilling);
        }

        private void txtHeatTransfer_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtHeatTransfer);
        }

        private void txtWidthOfRoof_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWidthOfRoof);
        }

        private void txtLengthOfRoof_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtLengthOfRoof);
        }


        private void txtAbsorptionCoefficient_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtAbsorptionCoefficient);
        }

        private void txtHeatTransferOfRoof_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtHeatTransferOfRoof);
        }

        private void txtWidthOfWall_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWidthOfWall);
        }

        private void txtLengthOfWall_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtLengthOfWall);
        }

        private void txtWidthOfFloor_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtWidthOfFloor);
        }

        private void txtLengthOfFloor_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtLengthOfFloor);
        }

        private void txtHeatTransferOfFloor_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtHeatTransferOfFloor);
        }
        private void txtEnergytOfEquipment_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtEnergytOfEquipment);
        }
        #endregion



        #endregion

    }
}
